package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.winery.UserWineryDrinkerData;


import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserWineryDrinkerDaoImpl extends TempDataObjectDao<UserWineryDrinkerData> {

    public static String getUserWineryDrinkerDataSql = "select * from `t_u_winery_drinker` where `userId`=? and `drinkerId`=?";
    public UserWineryDrinkerData getUserWineryDrinkerData(long userId,int drinkerId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserWineryDrinkerDataSql);
            int i = 1;
            call.setLong(i++, userId);
            call.setInt(i++, drinkerId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateUserWineryDrinkerDataSql = "update `t_u_winery_drinker` set friendlyValue=?,rewardList=?,createAt=?,updateAt=? where userId=? and drinkerId=?";
    @Override
    protected boolean updateData(UserWineryDrinkerData userWineryDrinkerData) {
        userWineryDrinkerData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userWineryDrinkerData.getFriendlyValue()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userWineryDrinkerData.getRewardList())));
        para.put(i++, new DbParameter(Types.TIMESTAMP, new Timestamp(userWineryDrinkerData.getCreateAt().getTime())));
        para.put(i++, new DbParameter(Types.TIMESTAMP, new Timestamp(userWineryDrinkerData.getUpdateAt().getTime())));
        para.put(i++, new DbParameter(Types.BIGINT, userWineryDrinkerData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userWineryDrinkerData.getDrinkerId()));
        if (getDb().executeNoneQuery(updateUserWineryDrinkerDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserWineryDrinkerDataSql = "insert into `t_u_winery_drinker`(userId,drinkerId,friendlyValue,rewardList,createAt,updateAt) values (?,?,?,?,?,?)";
    @Override
    protected boolean addData(UserWineryDrinkerData userWineryDrinkerData) {
        userWineryDrinkerData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userWineryDrinkerData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userWineryDrinkerData.getDrinkerId()));
        para.put(i++, new DbParameter(Types.INTEGER, userWineryDrinkerData.getFriendlyValue()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userWineryDrinkerData.getRewardList())));
        para.put(i++, new DbParameter(Types.TIMESTAMP, new Timestamp(userWineryDrinkerData.getCreateAt().getTime())));
        para.put(i++, new DbParameter(Types.TIMESTAMP, new Timestamp(userWineryDrinkerData.getUpdateAt().getTime())));
        if (getDb().executeNoneQuery(insertUserWineryDrinkerDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserWineryDrinkerData parseData(ResultSet rs) throws SQLException {
        UserWineryDrinkerData userWineryDrinkerData = new UserWineryDrinkerData();
        userWineryDrinkerData.setUserId(rs.getLong("userId"));
        userWineryDrinkerData.setDrinkerId(rs.getInt("drinkerId"));
        userWineryDrinkerData.setFriendlyValue(rs.getInt("friendlyValue"));
        userWineryDrinkerData.setRewardList(JSON.parseObject(rs.getString("rewardList"), new TypeReference<java.util.List<java.lang.Integer>>() {}));
        userWineryDrinkerData.setCreateAt(rs.getTimestamp("createAt"));
        userWineryDrinkerData.setUpdateAt(rs.getTimestamp("updateAt"));

        return userWineryDrinkerData;
    }


    public Map<Integer, UserWineryDrinkerData> getUserWineryDrinkerDataMap(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, UserWineryDrinkerData> userWineryDrinkerDataMap = new ConcurrentHashMap<>();
        String selectSql = "select * from t_u_winery_drinker where userId = ?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(selectSql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserWineryDrinkerData userWineryDrinkerData = parse(rs);
                userWineryDrinkerDataMap.put(userWineryDrinkerData.getDrinkerId(), userWineryDrinkerData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return userWineryDrinkerDataMap;
    }

}
